<h2>Why is this an issue?</h2>
<p>Nested ternaries are hard to read and can make the order of operations complex to understand.</p>
<pre>
function getReadableStatus(job) {
  return job.isRunning() ? "Running" : job.hasErrors() ? "Failed" : "Succeeded ";  // Noncompliant
}
</pre>
<p>Instead, use another line to express the nested operation in a separate statement.</p>
<pre>
function getReadableStatus(job) {
  if (job.isRunning()) {
    return "Running";
  }
  return job.hasErrors() ? "Failed" : "Succeeded";
}
</pre>
<h3>Exceptions</h3>
<p>This rule does not apply in JSX expressions to support conditional rendering and conditional attributes as long as the nesting happens in separate
JSX expression containers, i.e. JSX elements embedding JavaScript code, as shown below:</p>
<pre>
return (
&lt;&gt;
  {isLoading ? (
    &lt;Loader active /&gt;
  ) : (
    &lt;Panel label={isEditing ? 'Open' : 'Not open'}&gt;
      &lt;a&gt;{isEditing ? 'Close now' : 'Start now'}&lt;/a&gt;
      &lt;Checkbox onClick={!saving ? setSaving(saving =&gt; !saving) : null} /&gt;
    &lt;/Panel&gt;
  )}
&lt;/&gt;
);
</pre>
<p>If you have nested ternaries in the same JSX expression container, refactor your logic into a separate function like that:</p>
<pre>
function myComponent(condition) {
  if (condition &lt; 0) {
    return '&lt;DownSign&gt;it is negative&lt;/DownSign&gt;';
  } else if (condition &gt; 0) {
    return '&lt;UpSign&gt;it is positive&lt;/UpSign&gt;';
  } else {
    return '&lt;BarSign&gt;it is zero&lt;/BarSign&gt;';
  }
}

return (
  {myComponent(foo)}
);
</pre>
<h2>Resources</h2>
<h3>Articles &amp; blog posts</h3>
<ul>
  <li> Sonar - <a href="https://www.sonarsource.com/blog/stop-nesting-ternaries-javascript/">Stop nesting ternaries in JavaScript</a> </li>
</ul>
